package com.jn.erp.wechat.mp.config;

import com.jn.erp.wechat.cp.config.WxCpConfig;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/11/6 11:32
 */
@RequiredArgsConstructor
@Configuration
public class WxMpJnConfiguration {

    private final WxMpJnConfig wxMpConfig;
    private static WxMpJnConfig configMpFromYml;


    @PostConstruct
    public void init() {
        if (wxMpConfig != null && StringUtils.isNotBlank(wxMpConfig.getAppID())) {
            configMpFromYml = new WxMpJnConfig();
            BeanUtils.copyProperties(wxMpConfig, configMpFromYml);
        }

    }


    //jn服务号
    @Bean("wxMpJnService")
    public WxMpService wxMpJnService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        // 1. 创建WxCpConfigStorage对象
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        // 设置微信企业号的appId和secret
        wxMpConfigStorage.setAppId(configMpFromYml.getAppID());
        wxMpConfigStorage.setSecret(configMpFromYml.getSecret());
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;

    }

}
